// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HMAC_MD5.java


//EPS1000ND 在服务端的验证程序

package net.comm.encrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HMAC_MD5
{
    private byte digest[];
    private byte kIpad[];
    private byte kOpad[];
    private MessageDigest md5;
    private MessageDigest innerMD5;	

    public HMAC_MD5(byte abyte0[])
        throws NoSuchAlgorithmException
    {
        md5 = MessageDigest.getInstance("MD5");
        innerMD5 = MessageDigest.getInstance("MD5");
        int i = abyte0.length;
        if(i > 64)
        {
            md5.update(abyte0);
            abyte0 = md5.digest();
        }
        kIpad = new byte[64];
        kOpad = new byte[64];
        System.arraycopy(abyte0, 0, kIpad, 0, i);
        System.arraycopy(abyte0, 0, kOpad, 0, i);
        for(int j = 0; j < 64; j++)
        {
            kIpad[j] ^= 0x36;
            kOpad[j] ^= 0x5c;
        }

        clear();
    }

    public void addData(byte abyte0[])
    {
        addData(abyte0, 0, abyte0.length);
    }

    public void addData(byte abyte0[], int i, int j)
    {
        innerMD5.update(abyte0, i, j);
    }

    public void clear()
    {
        innerMD5.reset();
        innerMD5.update(kIpad);
        digest = null;
    }



    public byte[] sign()
    {
        md5.reset();
        digest = innerMD5.digest();
        md5.reset();
        md5.update(kOpad);
        md5.update(digest);
        digest = md5.digest();
        return digest;
    }

    public String toString()
    {
        if(digest == null)
            sign();
        StringBuffer stringbuffer = new StringBuffer();
        String s = "0123456789ABCDEF";
        byte abyte0[] = digest;
        for(int i = 0; i < 16; i++)
        {
            int j = abyte0[i] >>> 4 & 0xf;
            stringbuffer.append("0123456789ABCDEF".charAt(j));
            j = abyte0[i] & 0xf;
            stringbuffer.append("0123456789ABCDEF".charAt(j));
        }

        return stringbuffer.toString();
    }

    public boolean verify(byte abyte0[])
    {
        if(digest == null)
            sign();
        int i = abyte0.length;
        int j = digest.length;
        if(i != j)
            return false;
        for(int k = 0; k < i; k++)
            if(abyte0[k] != digest[k])
                return false;

        return true;
    }
    
    
    public static void main(String args[])
    {
        System.out.println("Test Vectors from RFC 2104 - HMAC: Keyed-Hashing for Message Authentication");
        System.out.println("This test uses HMAC-MD5.");
        System.out.println();
        System.out.println("Test #1:");
        byte abyte1[] = {
            11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
            11, 11, 11, 11, 11, 11
        };
        String s1 = "Hi There";
        String s = "0X9294727A3638BB1C13F48EF8158BFC9D";
        try
        {
            HMAC_MD5 hmac_md5 = new HMAC_MD5(abyte1);
            hmac_md5.addData(s1.getBytes());
            byte abyte0[] = hmac_md5.sign();
            System.out.println("Calculated hash 0X" + hmac_md5);
            System.out.println("  Expected hash " + s);
            System.out.println();
            System.out.println("Test #2:");
            byte abyte2[] = "Jefe".getBytes();
            String s2 = "what do ya want for nothing?";
            s = "0X750C783E6AB0B503EAA86E310A5DB738";
            hmac_md5 = new HMAC_MD5(abyte2);
            hmac_md5.addData(s2.getBytes());
            abyte0 = hmac_md5.sign();
            System.out.println("Calculated hash 0X" + hmac_md5);
            System.out.println("  Expected hash " + s);
            System.out.println();
            System.out.println("Test #3:");
            byte abyte3[] = {
                -86, -86, -86, -86, -86, -86, -86, -86, -86, -86, 
                -86, -86, -86, -86, -86, -86
            };
            byte abyte4[] = {
                -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, 
                -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, 
                -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, 
                -35, -35, -35, -35, -35, -35, -35, -35, -35, -35, 
                -35, -35, -35, -35, -35, -35, -35, -35, -35, -35
            };
            s = "0X56BE34521D144C88DBB8C733F0E8B3F6";
            byte abyte5[] = {
                86, -66, 52, 82, 29, 20, 76, -120, -37, -72, 
                -57, 51, -16, -24, -77, -10
            };
            hmac_md5 = new HMAC_MD5(abyte3);
            hmac_md5.addData(abyte4);
            abyte0 = hmac_md5.sign();
            System.out.println("Calculated hash 0X" + hmac_md5);
            System.out.println("  Expected hash " + s);
            System.out.println("Signature Verification: " + hmac_md5.verify(abyte5));
        }
        catch(NoSuchAlgorithmException nosuchalgorithmexception)
        {
            nosuchalgorithmexception.printStackTrace();
        }
    }

}
